package one.stand.eureka;


import com.netflix.appinfo.ApplicationInfoManager;
import lombok.extern.slf4j.Slf4j;
import one.stand.eureka.info.ApplicationInfoManagerHandler;
import one.stand.eureka.info.DefaultServiceInfo;
import one.stand.eureka.info.IServiceInfo;
import one.stand.eureka.info.ServiceConfigReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration;
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.Resource;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.io.IOException;
import java.util.TimeZone;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author Administrator
 */
@EnableEurekaServer
@SpringBootApplication(exclude = {MongoAutoConfiguration.class, MongoDataAutoConfiguration.class})
@EnableAsync
@Slf4j
public class OneEurekaApplication {

    public static void main(String[] args) {
        TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));
        SpringApplication.run(OneEurekaApplication.class, args);
    }

    @Value("${one.env.deploy}")
    protected String deploy;
    @Autowired
    private ApplicationContext applicationContext;

    @Bean
    public ServiceConfigReader serviceConfigReader() {
        try {
            Resource resource = applicationContext.getResource("file:" + deploy);
            log.info(resource.getFile().getAbsolutePath());
            return new ServiceConfigReader(resource.getFile().getAbsolutePath());
        } catch (IOException e) {
            log.error("init error");
            System.exit(2);
        }
        return null;
    }

    @Bean
    public ApplicationInfoManagerHandler applicationInfoManagerHandler(ApplicationInfoManager applicationInfoManager) {
        return new ApplicationInfoManagerHandler(applicationInfoManager);
    }

    @Bean
    public IServiceInfo serviceInfo(ApplicationInfoManagerHandler applicationInfoManagerHandler) {
        return new DefaultServiceInfo(applicationInfoManagerHandler, serviceConfigReader());
    }

    @Bean("asyncServiceExecutor")
    public Executor asyncServiceExecutor() {
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        //线程池核心线程数
        taskExecutor.setCorePoolSize(5);
        //线程池最大线程数
        taskExecutor.setMaxPoolSize(10);
        //缓冲队列
        taskExecutor.setQueueCapacity(20);
        //线程名称前缀
        taskExecutor.setThreadNamePrefix("eureka-task-service-");
        //线程空闲后的最大存活时间
        taskExecutor.setKeepAliveSeconds(50);
        taskExecutor.setWaitForTasksToCompleteOnShutdown(true);
        taskExecutor.setAwaitTerminationSeconds(60);
        //rejection-policy：当pool已经达到max size的时候，如何处理新任务
        //CALLER_RUNS：不在新线程中执行任务，而是由调用者所在的线程来执行
        //对拒绝task的处理策略
        taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        //执行初始化
        taskExecutor.initialize();

        log.info("async task init");
        return taskExecutor;
    }
}
